<?php
/* 
 * This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
/**
 * $Rev: 29 $
 * $Id: class.mysqldata.php 29 2011-06-23 07:40:48Z superbounou $
 * $Author: superbounou $
 * $Date: 2011-06-23 07:40:48 +0000 (Thu, 23 Jun 2011) $
 * 
 * Control MySQL interaction
 */
abstract class MySQLData {

	private static $mysqlConnection;

	private static $debugQuery = array ();

	/**
	 * 
	 */
	private static function setConnection($host = MYSQL_HOST, $db = MYSQL_DB, $user = MYSQL_USER, $pwd = MYSQL_PWD) {
		if (!isset (self :: $mysqlConnection)) {
			assert('/* SQL : host and user validity */strlen($host) && strlen($user)');
			self :: $mysqlConnection = mysql_connect($host, $user, $pwd);
			assert('/*MySQL connection*/self::$mysqlConnection != false');
			$state = mysql_select_db($db, self :: $mysqlConnection);
			assert('/*MySQL select db*/$state');
		}
	}

	protected $table;

	/**
	 * 
	 */
	public function __construct($host = MYSQL_HOST, $db = MYSQL_DB, $user = MYSQL_USER, $pwd = MYSQL_PWD) {
		self :: setConnection($host, $db, $user, $pwd);
		/* note : it does not work if it's sensitive case */
		$this->table = get_class($this);
	}

	public function getTable() {
		return $this->table;
	}

	public function setTable($table) {
		$this->table = $table;
	}

	public function getFields() {
		return $this->fetch('DESC ' . $this->getTable());
	}

	public static function debug() {
		if (strlen(mysql_error())) {
			MySQLData :: $debugQuery[] = '<strong>MySQL Error : </strong>' . mysql_error();
		}
		Helper :: displayDebug(MySQLData :: $debugQuery);
	}

	public function query($sql) {
		assert('/* SQL : empty query */strlen($sql) > 0');
		assert('/* MySQL connection is not available */is_resource(self::$mysqlConnection)');
		$timeStart = microtime(true);
		$statement = mysql_query($sql, self :: $mysqlConnection);
		$timeEnd = microtime(true);
		$time = $timeEnd - $timeStart;
		if (ACTIVE_DEBUG) {
			MySQLData :: $debugQuery[] = $sql . ' <b>(' . round($time, 4) . ' sec)</b>';
		}
		assert('/*SQL : ' . $sql . '*/$statement != false');
		return $statement;
	}

	public function fetch($sql) {
		assert(strlen($sql) > 0);
		$mysqlState = $this->query($sql);
		$rows = array ();
		while ($row = mysql_fetch_assoc($mysqlState)) {
			$rows[] = $row;
		}
		return $rows;
	}

	public function get($condition = '1', $fields = '*') {
		assert('/*condition not null*/strlen($condition) > 0');
		assert(strlen($fields) == '*' || strlen($fields));
		return $this->fetch('SELECT ' . $fields . ' FROM ' . $this->getTable() . ' WHERE ' . $condition);
	}

	public function del($condition = '1') {
		assert(strlen($condition) > 0);
		return $this->query('DELETE FROM ' . $this->getTable() . ' WHERE ' . $condition);
	}

	public function update($fields, $condition = '1') {
		assert('strlen($condition) > 0 && strlen($fields) > 0');
		return $this->query('UPDATE ' . $this->getTable() . ' SET ' . $fields . ' WHERE ' . $condition);
	}

	public function add($fields) {
		assert('strlen($fields) > 0');
		return $this->query('INSERT INTO ' . $this->getTable() . ' VALUES (' . $fields . ')');
	}

	public function getField($condition, $field) {
		assert('/*testing field name : ' . $field . ' */preg_match(\'/[a-zA-Z_\-0-9]+/\', $field)');
		$results = array ();
		foreach ($this->get($condition, $field) as $line) {
			$results[] = $line[$field];
		}
		return $results;
	}
}
?>